// 获取assets静态资源
export const url = (url: string) => {
    return new URL(`../assets/images/${url}`, import.meta.url).href
}

/**
 * 只实现了#ffffff、rgb、rgba 三种形式
 */
export const fade = (color: string, opacity: number) => {
    if (color.startsWith("#")) {
        return 'rgba(' +
            parseInt('0x' + color.slice(1, 3)) + ',' +
            parseInt('0x' + color.slice(3, 5)) + ',' +
            parseInt('0x' + color.slice(5, 7)) + ',' +
            opacity + ')'
    } else if (color.startsWith("rgb")) {
        const rgbaArr = color.replace(/rgba?\(|\)|\s/g, '').split(',').map(x => +x);
        const fianlOpacity = rgbaArr[3] ? rgbaArr[3] * opacity : opacity;
        return `rgba(${rgbaArr[0]},${rgbaArr[1]},${rgbaArr[2]},${fianlOpacity})`;
    }
    return color;
}

/**
 * 将str中的{}占位符替换为data中的值
 * @param str 带占位符的字符串
 * @param data 数据对象
 */
export const replacePlaceholders = (str: string, data: { [key: string]: any }) => {
    return str.replace(/\{([\w.]+)}/g, (match: any, key: string) => {
        const keys = key.split('.');
        let value = data;
        for (let i = 0; i < keys.length; i++) {
            if (Object.prototype.hasOwnProperty.call(value, keys[i])) {
                value = value[keys[i]];
            } else {
                return match;
            }
        }
        return value;
    });
}

/**
 * 获取行政编码级别,1-省 2-市 3-县
 */
export const getZoneLevel = (zoneCode: string) => {
    const shortZoneCode = removeZoneSuffix(zoneCode);
    const length = shortZoneCode.length;
    //4位行政编码 且以90结尾的为 省直辖县
    return (length === 4 && shortZoneCode.endsWith('90')) ? 3 : length / 2;
}

/**
 * 去除zoneCode 后缀0
 * @param zoneCode 行政编码
 */
export const removeZoneSuffix = (zoneCode: string) => {
    return zoneCode.replace(/(00)*$/g, "");
}

//获取行政区划名称
export const getRegionName: any = (zoneCode: string, regions: any[]) => {
    const shortCode = zoneCode.replace(/(00)+$/g, '');
    const region = regions.filter(item => item.value.startsWith(shortCode));
    if (region?.length) {
        if (zoneCode === region[0].value) {
            return region[0].label;
        } else {
            return region[0].children?.length ? getRegionName(zoneCode, region[0].children) : null
        }
    }
    return null;
}

export const getType = (value: any) =>
  Object.prototype.toString.call(value).slice(8, -1)


// 对不知道类型的值进行浅拷贝
export const ShallowCopy = (value?: any) => {
    const type = getType(value)
    let res
    switch (type) {
        case 'Object':
            res = { ...value }
            break
        case 'Array':
            res = [...value]
            break
        default:
            res = value
    }
    return res
}

export const formatClass = (classes: (string | undefined)[]) => {
    return classes.join(' ')
}

// 转换数字格式
export const toThousands = (num: number) => {
    let _num = (num || 0).toString()
    let res = ''
    const pointIndex = _num.indexOf('.')
    if (pointIndex > -1) {
        res = _num.slice(pointIndex)
        _num = _num.slice(0, pointIndex)
    }
    while (_num.length > 3) {
        res = ',' + _num.slice(-3) + res
        _num = _num.slice(0, _num.length - 3)
    }
    if (_num) {
        res = _num + res
    }
    return res
}

export const getUrlParams = (e: any) => {
    var t = new RegExp('(^|&)' + e + '=([^&]*)(&|$)', 'i')
      , n = window.location.search.substr(1).match(t)
    return null != n ? unescape(n[2]) : null
}
